﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.BaseInfoServiceRef;

    public class SKU : BasePage
    {
        protected Button btnAddAlternateSku;
        protected Button btnAddPickLoc;
        protected Button btnAddSubstituteSku;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteAlternateSku;
        protected Button btnDeletePickLoc;
        protected Button btnDeleteSubstituteSku;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkCheckProduceDate;
        protected CheckBox chkIsActive;
        protected CheckBox chkIsSalePromption;
        protected CheckBox chkMoveable;
        protected CheckBox chkOpportunistic;
        protected CheckBox chkShelfLifeIndicator;
        protected DropDownList ddlAllocateRuleID;
        protected DropDownList ddlCalculatePutawayLocation;
        protected DropDownList ddlCCDiscrepanCyrule;
        protected DropDownList ddlFreightClass;
        protected DropDownList ddlPreallocateRuleID;
        protected DropDownList ddlPutawayRuleID;
        protected DropDownList ddlReciptValidationID;
        protected DropDownList ddlRotationRuleID;
        protected DropDownList ddlShelfLifeCodeType;
        protected DropDownList ddlTransportMode;
        protected HtmlForm F2061;
        protected GridView gvAlternateSku;
        protected GridView gvPickLoc;
        protected GridView gvSubstituteSku;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlImage img1;
        protected HtmlImage imgLastCycleCount;
        protected HtmlImage imgPutawayLoc;
        protected HtmlImage imgQCLocOut;
        protected HtmlImage imgReceiptInspectionLoc;
        protected HtmlImage imgReturnsLoc;
        protected HtmlImage imgZoneID;
        protected Label lblABC;
        protected Label lblAllocateRuleID;
        protected Label lblbudf1;
        protected Label lblbudf10;
        protected Label lblbudf2;
        protected Label lblbudf3;
        protected Label lblbudf4;
        protected Label lblbudf5;
        protected Label lblbudf6;
        protected Label lblbudf7;
        protected Label lblbudf8;
        protected Label lblbudf9;
        protected Label lblCalculatePutawayLocation;
        protected Label lblCartonGroup;
        protected Label lblCCDiscrepanCyrule;
        protected Label lblColor;
        protected Label lblCostPrice;
        protected Label lblCrossdock;
        protected Label lblCube;
        protected Label lblCustomSkuNo;
        protected Label lblCycleCount;
        protected Label lblDaysToExpire;
        protected Label lblDefaultInUom;
        protected Label lblDefaultOutUom;
        protected Label lblDescr;
        protected Label lblDescr2;
        protected Label lblFreightClass;
        protected Label lblGrossWeight;
        protected Label lblHazmatCode;
        protected Label lblHeight;
        protected Label lblLastCycleCount;
        protected Label lblLength;
        protected Label lblLotTemplate;
        protected Label lblMaxQty;
        protected Label lblMinQty;
        protected Label lblMsg;
        protected Label lblNetPrice;
        protected Label lblNetWeight;
        protected Label lblOriginCountry;
        protected Label lblOwnerID;
        protected Label lblPack;
        protected Label lblPreallocateRuleID;
        protected Label lblPutawayLoc;
        protected Label lblPutawayRuleID;
        protected Label lblQC;
        protected Label lblQCLocOut;
        protected Label lblReceiptInspectionLoc;
        protected Label lblReceiptValidationRuleID;
        protected Label lblReceiptValidationTemplate;
        protected Label lblRemark1;
        protected Label lblRemark2;
        protected Label lblReOrderQty;
        protected Label lblReturnsLoc;
        protected Label lblRFDefaultPack;
        protected Label lblRotationRuleID;
        protected Label lblSalePrice;
        protected Label lblShelfLife;
        protected Label lblShelfLifeCodeType;
        protected Label lblShelfLifeOnReceiving;
        protected Label lblSize;
        protected Label lblSku;
        protected Label lblSkuClass;
        protected Label lblSkuGroup;
        protected Label lblSkuGroup2;
        protected Label lblSkuID;
        protected Label lblStyle;
        protected Label lblTareWeight;
        protected Label lblTariffID;
        protected Label lblTransportMode;
        protected Label lblUserDefine1;
        protected Label lblUserDefine10;
        protected Label lblUserDefine2;
        protected Label lblUserDefine3;
        protected Label lblUserDefine4;
        protected Label lblUserDefine5;
        protected Label lblUserDefine6;
        protected Label lblUserDefine7;
        protected Label lblUserDefine8;
        protected Label lblUserDefine9;
        protected Label lblWidth;
        protected Label lblZoneID;
        protected Label lbRelOrderPoint;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        protected Label one5;
        protected Label one6;
        protected Label one7;
        protected ScriptManager ScriptManager1;
        protected TextBox txtABC;
        protected TextBox txtbudf1;
        protected TextBox txtbudf10;
        protected TextBox txtbudf2;
        protected TextBox txtbudf3;
        protected TextBox txtbudf4;
        protected TextBox txtbudf5;
        protected TextBox txtbudf6;
        protected TextBox txtbudf7;
        protected TextBox txtbudf8;
        protected TextBox txtbudf9;
        protected TextBox txtCartonGroup;
        protected TextBox txtColor;
        protected TextBox txtCostPrice;
        protected TextBox txtCube;
        protected TextBox txtCustomSkuNo;
        protected TextBox txtDaysToExpire;
        protected TextBox txtDefaultInUom;
        protected TextBox txtDefaultOutUom;
        protected TextBox txtDescr;
        protected TextBox txtDescr2;
        protected TextBox txtGrossWeight;
        protected TextBox txtHazmatCode;
        protected TextBox txtHeight;
        protected TextBox txtLastCycleCount;
        protected TextBox txtLength;
        protected TextBox txtLotTemplate;
        protected TextBox txtMaxQty;
        protected TextBox txtMinQty;
        protected TextBox txtNetPrice;
        protected TextBox txtNetWeight;
        protected TextBox txtOriginCountry;
        protected TextBox txtOwnerID;
        protected TextBox txtPack;
        protected TextBox txtPutawayLoc;
        protected TextBox txtQCLocOut;
        protected TextBox txtReceiptInspectionLoc;
        protected TextBox txtReceiptValidationTemplate;
        protected TextBox txtRemark1;
        protected TextBox txtRemark2;
        protected TextBox txtReOrderPoint;
        protected TextBox txtReOrderQty;
        protected TextBox txtReturnsLoc;
        protected TextBox txtRFDefaultPack;
        protected TextBox txtSalePrice;
        protected TextBox txtShelfLife;
        protected TextBox txtShelfLifeOnReceiving;
        protected TextBox txtSize;
        protected TextBox txtSkuClass;
        protected TextBox txtSkuGroup;
        protected TextBox txtSkuGroup2;
        protected TextBox txtSkuID;
        protected TextBox txtStyle;
        protected TextBox txtTareWeight;
        protected TextBox txtTariffID;
        protected TextBox txtUserDefine1;
        protected TextBox txtUserDefine10;
        protected TextBox txtUserDefine2;
        protected TextBox txtUserDefine3;
        protected TextBox txtUserDefine4;
        protected TextBox txtUserDefine5;
        protected TextBox txtUserDefine6;
        protected TextBox txtUserDefine7;
        protected TextBox txtUserDefine8;
        protected TextBox txtUserDefine9;
        protected TextBox txtWidth;
        protected TextBox txtZoneID;
        protected UpdatePanel up1;

        protected void btnAddAlternateSku_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_001");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_004");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnOwnerID.Value) && string.IsNullOrEmpty(this.hdnSkuID.Value))
                {
                    this.InsertSkuInfo();
                    this.hdnSeqNo.Value = "10";
                }
                base.Response.Redirect(string.Format("AlternateSku.aspx?from={0}&ID1={1}&ID2={2}&SeqNO={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()), base.UrlEncode(this.hdnSeqNo.Value) }), true);
            }
        }

        protected void btnAddPickLoc_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("SkuPickLoc.aspx?from={0}&ID={1}&ID2={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim())), true);
        }

        protected void btnAddSubstituteSku_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_001");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_004");
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnOwnerID.Value) && string.IsNullOrEmpty(this.hdnSkuID.Value))
                {
                    this.InsertSkuInfo();
                    this.hdnSeqNo.Value = "10";
                }
                base.Response.Redirect(string.Format("SubstituteSku.aspx?from={0}&ID={1}&ID2={2}&SeqNO={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()), base.UrlEncode(this.hdnSeqNo.Value) }), true);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtSkuID.Text = "";
            this.txtSkuID.Enabled = true;
            this.txtOwnerID.Text = "";
            this.txtOwnerID.Enabled = true;
            this.ddlPutawayRuleID.SelectedValue = "STD";
            this.txtZoneID.Text = "";
            this.txtMinQty.Text = "";
            this.txtABC.Text = "";
            this.ddlCalculatePutawayLocation.SelectedValue = "Manual";
            this.txtDefaultInUom.Text = "";
            this.txtPutawayLoc.Text = "Stage";
            this.txtMaxQty.Text = "";
            this.txtDefaultOutUom.Text = "";
            this.ddlPreallocateRuleID.SelectedValue = "STD";
            this.txtReceiptInspectionLoc.Text = "";
            this.txtReOrderPoint.Text = "";
            this.txtRFDefaultPack.Text = "";
            this.ddlAllocateRuleID.SelectedValue = "STD";
            this.txtQCLocOut.Text = "";
            this.ddlRotationRuleID.SelectedValue = "STD";
            this.txtReturnsLoc.Text = "";
            this.txtABC.Text = "";
            this.txtLastCycleCount.Text = "";
            SkuInfo skuInfo = base.DataEntityViewState as SkuInfo;
            skuInfo.AlternateSkuInfos = null;
            this.gvAlternateSku.DataSource = null;
            this.gvAlternateSku.DataBind();
            skuInfo.SubstituteSkuInfos = null;
            this.gvSubstituteSku.DataSource = null;
            this.gvSubstituteSku.DataBind();
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()) && !string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                try
                {
                    List<string> ownerID = new List<string>();
                    List<string> skuID = new List<string> {
                        this.txtOwnerID.Text.Trim(),
                        this.txtSkuID.Text.Trim()
                    };
                    this.lblMsg.Text = "货品 " + BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(ownerID, skuID, new List<string>(), "SKU") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnDeleteAlternateSku_Click(object sender, EventArgs e)
        {
            List<string> ownerID = base.GetSelectedIDsInGridView(this.gvAlternateSku, "OwnerID");
            List<string> skuID = base.GetSelectedIDsInGridView(this.gvAlternateSku, "SkuID");
            List<string> altSkuID = base.GetSelectedIDsInGridView(this.gvAlternateSku, "AltSkuID");
            try
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_009") + BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(ownerID, skuID, altSkuID, "ALTERNATE_SKU") + base.GetGlobalMsgResource("C006");
                this.SetSkuInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,5);</script>", false);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_010") + ex.Message;
            }
        }

        protected void btnDeletePickLoc_Click(object sender, EventArgs e)
        {
            List<string> ownerID = base.GetSelectedIDsInGridView(this.gvPickLoc, "OwnerID");
            List<string> skuID = base.GetSelectedIDsInGridView(this.gvPickLoc, "SkuID");
            List<string> Loc = base.GetSelectedIDsInGridView(this.gvPickLoc, "Loc");
            List<string> qty = base.GetSelectedIDsInGridView(this.gvPickLoc, "Qty");
            if (ownerID.Count > 0)
            {
                for (int i = 0; ownerID.Count > i; i++)
                {
                    if (decimal.Parse(qty[i]) > 0M)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2110_013", new List<string> { Loc[i], skuID[i] });
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',5,7);</script>", false);
                        return;
                    }
                }
            }
            try
            {
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(ownerID, skuID, Loc, "INV_SKU_LOC") + base.GetGlobalMsgResource("C006");
                this.SetSkuInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',5,7);</script>", false);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_014") + ex.Message;
            }
        }

        protected void btnDeleteSubstituteSku_Click(object sender, EventArgs e)
        {
            List<string> ownerID = base.GetSelectedIDsInGridView(this.gvSubstituteSku, "OwnerID");
            List<string> skuID = base.GetSelectedIDsInGridView(this.gvSubstituteSku, "SkuID");
            List<string> subSkuID = base.GetSelectedIDsInGridView(this.gvSubstituteSku, "SubstituteSkuID");
            try
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_011") + BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(ownerID, skuID, subSkuID, "SUBSTITUTE_SKU") + base.GetGlobalMsgResource("C006");
                this.SetSkuInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,7);</script>", false);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_012") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("SkuQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("SkuList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_001");
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_004");
            }
            else if (string.IsNullOrEmpty(this.txtPutawayLoc.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_005");
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,5);</script>", false);
            }
            else if (string.IsNullOrEmpty(this.txtPack.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_006");
            }
            else if (string.IsNullOrEmpty(this.txtLotTemplate.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_007");
            }
            else if (string.IsNullOrEmpty(this.txtTariffID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_008");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateSkuInfo();
            }
            else
            {
                this.InsertSkuInfo();
            }
        }

        protected string FormatReceiptUrl(string url, string ownerID, string skuID, string altSkuID)
        {
            return string.Format(url, new object[] { this.hdnFrom.Value.Trim(), base.UrlEncode(ownerID), base.UrlEncode(skuID), base.UrlEncode(altSkuID) });
        }

        protected string FormatSubStituteSkuUrl(string url, string OwnerID, string skuID, string SubstituteSkuID, string SeqNo)
        {
            return string.Format(url, new object[] { base.UrlEncode(OwnerID), base.UrlEncode(skuID), base.UrlEncode(SubstituteSkuID), base.UrlEncode(SeqNo) });
        }

        protected void gdvPickLoc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        private SkuInfo GetSkuInfoFromForm(SkuInfo skuInfo)
        {
            skuInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            skuInfo.ShelfLifeIndicator = this.chkShelfLifeIndicator.Checked ? "Y" : "N";
            skuInfo.IsSalePromption = this.chkIsSalePromption.Checked ? "Y" : "N";
            skuInfo.OwnerID = this.txtOwnerID.Text.Trim();
            skuInfo.SkuID = this.txtSkuID.Text.Trim();
            skuInfo.SkuClassID = this.txtSkuClass.Text.Trim();
            skuInfo.Descr = this.txtDescr.Text.Trim();
            skuInfo.HazmatCode = this.txtHazmatCode.Text.Trim();
            skuInfo.ShelfLifeCodeType = this.ddlShelfLifeCodeType.SelectedValue;
            skuInfo.Descr2 = this.txtDescr2.Text.Trim();
            skuInfo.CustomSkuNo = this.txtCustomSkuNo.Text.Trim();
            skuInfo.ShelfLifeOnReceiving = int.Parse((this.txtShelfLifeOnReceiving.Text.Trim() == "") ? "0" : this.txtShelfLifeOnReceiving.Text.Trim());
            skuInfo.PackID = this.txtPack.Text.Trim();
            skuInfo.CartonGroup = this.txtCartonGroup.Text.Trim();
            skuInfo.CountryOfOrigin = this.txtOriginCountry.Text.Trim();
            skuInfo.ShelfLife = int.Parse((this.txtShelfLife.Text.Trim() == "") ? "0" : this.txtShelfLife.Text.Trim());
            skuInfo.LotTemplateID = this.txtLotTemplate.Text.Trim();
            skuInfo.DaysToExpire = int.Parse((this.txtDaysToExpire.Text.Trim() == "") ? "0" : this.txtDaysToExpire.Text.Trim());
            skuInfo.Length = float.Parse((this.txtLength.Text.Trim() == "") ? "0" : this.txtLength.Text.Trim());
            skuInfo.Width = float.Parse((this.txtWidth.Text.Trim() == "") ? "0" : this.txtWidth.Text.Trim());
            skuInfo.Height = float.Parse((this.txtHeight.Text.Trim() == "") ? "0" : this.txtHeight.Text.Trim());
            skuInfo.Cube = float.Parse((this.txtCube.Text.Trim() == "") ? "0" : this.txtCube.Text.Trim());
            skuInfo.NetWgt = float.Parse((this.txtNetWeight.Text.Trim() == "") ? "0" : this.txtNetWeight.Text.Trim());
            skuInfo.GrossWgt = float.Parse((this.txtGrossWeight.Text.Trim() == "") ? "0" : this.txtGrossWeight.Text.Trim());
            skuInfo.TareWgt = float.Parse((this.txtTareWeight.Text.Trim() == "") ? "0" : this.txtTareWeight.Text.Trim());
            skuInfo.SkuSize = this.txtSize.Text.Trim();
            skuInfo.SkuColor = this.txtColor.Text.Trim();
            skuInfo.SkuStyle = this.txtStyle.Text.Trim();
            skuInfo.CostPrice = decimal.Parse((this.txtCostPrice.Text.Trim() == "") ? "0" : this.txtCostPrice.Text.Trim());
            skuInfo.SalePrice = decimal.Parse((this.txtSalePrice.Text.Trim() == "") ? "0" : this.txtSalePrice.Text.Trim());
            skuInfo.NetPrice = decimal.Parse((this.txtNetPrice.Text.Trim() == "") ? "0" : this.txtNetPrice.Text.Trim());
            skuInfo.DefaultInUom = this.txtDefaultInUom.Text.Trim();
            skuInfo.DefaultOutUom = this.txtDefaultOutUom.Text.Trim();
            skuInfo.RfDefaultPack = this.txtRFDefaultPack.Text.Trim();
            skuInfo.ReceiptValidationTemplate = "";
            skuInfo.ReceiptValidationID = this.ddlReciptValidationID.SelectedValue;
            skuInfo.PutawayRuleID = this.ddlPutawayRuleID.SelectedValue;
            skuInfo.CalculatePutawayLocation = this.ddlCalculatePutawayLocation.SelectedValue;
            skuInfo.PreallocateRuleID = this.ddlPreallocateRuleID.SelectedValue;
            skuInfo.AllocateRuleID = this.ddlAllocateRuleID.SelectedValue;
            skuInfo.RotationRuleID = this.ddlRotationRuleID.SelectedValue;
            skuInfo.Abc = this.txtABC.Text.Trim();
            skuInfo.CcDiscrepanCyrule = this.ddlCCDiscrepanCyrule.SelectedValue;
            skuInfo.LastCycleCount = (this.txtLastCycleCount.Text.Trim() == "") ? DateTime.Now : DateTime.Parse(this.txtLastCycleCount.Text.Trim());
            skuInfo.QcLoc = "";
            skuInfo.StdOrderCost = 0f;
            skuInfo.CarryCost = 0f;
            skuInfo.ReorderPoint = 0;
            skuInfo.ReorderQty = 0M;
            skuInfo.CycleCountFrequency = 0;
            skuInfo.ZoneID = this.txtZoneID.Text.Trim();
            skuInfo.PutawayLoc = this.txtPutawayLoc.Text.Trim();
            skuInfo.ReceiptInspectionLoc = this.txtReceiptInspectionLoc.Text.Trim();
            skuInfo.QcLocOut = this.txtQCLocOut.Text.Trim();
            skuInfo.ReturnsLoc = this.txtReturnsLoc.Text.Trim();
            skuInfo.MinQty = decimal.Parse((this.txtMinQty.Text.Trim() == "") ? "0" : this.txtMinQty.Text.Trim());
            skuInfo.MaxQty = decimal.Parse((this.txtMaxQty.Text.Trim() == "") ? "0" : this.txtMaxQty.Text.Trim());
            skuInfo.SkuGroup = "";
            skuInfo.SkuGroup2 = "";
            skuInfo.Udf1 = this.txtUserDefine1.Text.Trim();
            skuInfo.Udf2 = this.txtUserDefine2.Text.Trim();
            skuInfo.Udf3 = this.txtUserDefine3.Text.Trim();
            skuInfo.Udf4 = this.txtUserDefine4.Text.Trim();
            skuInfo.Udf5 = this.txtUserDefine5.Text.Trim();
            skuInfo.Udf6 = this.txtUserDefine6.Text.Trim();
            skuInfo.Udf7 = this.txtUserDefine7.Text.Trim();
            skuInfo.Udf8 = this.txtUserDefine8.Text.Trim();
            skuInfo.Udf9 = this.txtUserDefine9.Text.Trim();
            skuInfo.Udf10 = this.txtUserDefine10.Text.Trim();
            skuInfo.BUdf1 = this.txtbudf1.Text.Trim();
            skuInfo.BUdf2 = this.txtbudf2.Text.Trim();
            skuInfo.BUdf3 = this.txtbudf3.Text.Trim();
            skuInfo.BUdf4 = this.txtbudf4.Text.Trim();
            skuInfo.BUdf5 = this.txtbudf5.Text.Trim();
            skuInfo.BUdf6 = this.txtbudf6.Text.Trim();
            skuInfo.BUdf7 = this.txtbudf7.Text.Trim();
            skuInfo.BUdf8 = this.txtbudf8.Text.Trim();
            skuInfo.BUdf9 = this.txtbudf9.Text.Trim();
            skuInfo.BUdf10 = this.txtbudf10.Text.Trim();
            skuInfo.Remark2 = this.txtRemark2.Text.Trim();
            skuInfo.Remark1 = this.txtRemark1.Text.Trim();
            skuInfo.TariffID = this.txtTariffID.Text.Trim();
            skuInfo.SkuGroup = this.txtSkuGroup.Text;
            skuInfo.SkuGroup2 = this.txtSkuGroup2.Text;
            skuInfo.TransportMode = this.ddlTransportMode.SelectedValue;
            if (!string.IsNullOrEmpty(this.txtReOrderQty.Text))
            {
                skuInfo.ReorderQty = decimal.Parse(this.txtReOrderQty.Text);
            }
            if (!string.IsNullOrEmpty(this.txtReOrderPoint.Text))
            {
                skuInfo.ReorderPoint = int.Parse(this.txtReOrderPoint.Text);
            }
            skuInfo.FreightClass = this.ddlFreightClass.SelectedValue;
            skuInfo.ReceiptValidationTemplate = this.txtReceiptValidationTemplate.Text;
            return skuInfo;
        }

        protected void gvAlternateSku_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as SkuInfo).AlternateSkuInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,7) </script>", false);
        }

        protected void gvAlternateSku_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void gvAlternateSku_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                SkuInfo skuInfo = base.DataEntityViewState as SkuInfo;
                DataView dv = CADataConverter.ToDataTable(skuInfo.AlternateSkuInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvAlternateSku, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,7);</script>", false);
            }
        }

        protected void gvPickLoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as SkuInfo).InvSkuLocInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',5,7) </script>", false);
        }

        protected void gvPickLoc_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                SkuInfo skuInfo = base.DataEntityViewState as SkuInfo;
                DataView dv = CADataConverter.ToDataTable(skuInfo.InvSkuLocInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvPickLoc, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',5,7);</script>", false);
            }
        }

        protected void gvSubstituteSku_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as SkuInfo).SubstituteSkuInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',4,7) </script>", false);
        }

        protected void gvSubstituteSku_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void gvSubstituteSku_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                SkuInfo skuInfo = base.DataEntityViewState as SkuInfo;
                DataView dv = CADataConverter.ToDataTable(skuInfo.SubstituteSkuInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvSubstituteSku, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,7);</script>", false);
            }
        }

        private void InitDropDownList()
        {
            base.BindData2DDLBySysCode(this.ddlCalculatePutawayLocation, "TMCalculateLoc");
            base.BindData2DDLBySysCode(this.ddlShelfLifeCodeType, "ShelfLifeType");
            base.BindData2DDLByRules(this.ddlPutawayRuleID, "PutawayRule");
            base.BindData2DDLByRules(this.ddlPreallocateRuleID, "PreallocateRule");
            base.BindData2DDLByRules(this.ddlAllocateRuleID, "AllocateRule");
            base.BindData2DDLByRules(this.ddlRotationRuleID, "RotationRule");
            base.BindData2DDLBySysCode(this.ddlTransportMode, "TransportMethod");
            base.BindData2DDLBySysCode(this.ddlFreightClass, "FreightClass");
            base.BindData2DDLByReceiptValidation(this.ddlReciptValidationID);
            base.BindData2DDLByCycleCountRule(this.ddlCCDiscrepanCyrule);
        }

        private void InsertSkuInfo()
        {
            try
            {
                SkuInfo skuInfo = new SkuInfo();
                if (base.DataEntityViewState != null)
                {
                    int i;
                    skuInfo = base.DataEntityViewState as SkuInfo;
                    if ((skuInfo.AlternateSkuInfos != null) && (skuInfo.AlternateSkuInfos.Count > 0))
                    {
                        for (i = 0; i < skuInfo.AlternateSkuInfos.Count; i++)
                        {
                            skuInfo.AlternateSkuInfos[i].OwnerID = this.txtOwnerID.Text.Trim();
                            skuInfo.AlternateSkuInfos[i].SkuID = this.txtSkuID.Text.Trim();
                        }
                    }
                    if ((skuInfo.SubstituteSkuInfos != null) && (skuInfo.SubstituteSkuInfos.Count > 0))
                    {
                        for (i = 0; i < skuInfo.SubstituteSkuInfos.Count; i++)
                        {
                            skuInfo.SubstituteSkuInfos[i].OwnerID = this.txtOwnerID.Text.Trim();
                            skuInfo.SubstituteSkuInfos[i].SkuID = this.txtSkuID.Text.Trim();
                        }
                    }
                }
                skuInfo = this.GetSkuInfoFromForm(skuInfo);
                skuInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(skuInfo, "SKU") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = skuInfo;
                this.hdnFlag.Value = "1";
                this.txtSkuID.Enabled = false;
                this.txtOwnerID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            this.InitDropDownList();
            if (!(string.IsNullOrEmpty(base.Request.QueryString["ID"]) || string.IsNullOrEmpty(base.Request.QueryString["ID2"])))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnSkuID.Value = base.Request.QueryString["ID2"].ToString();
                this.hdnFlag.Value = "1";
                this.SetSkuInfo2Form();
            }
            else
            {
                base.SetDefaultOwner(this.txtOwnerID);
                this.txtOwnerID_TextChanged(null, null);
                this.txtPack.Text = "STD";
                this.txtLotTemplate.Text = "STD";
                this.txtTariffID.Text = "STD";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["currTab"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["currTab"].ToString() + ",7);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSkuInfo2Form()
        {
            try
            {
                string ownerID = this.hdnOwnerID.Value;
                string skuID = this.hdnSkuID.Value;
                SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(ownerID, skuID, "", "SKU") as SkuInfo;
                base.DataEntityViewState = skuInfo;
                if (skuInfo != null)
                {
                    this.SetSkuInfo2Form(skuInfo);
                    base.BindData2GridView(this.gvAlternateSku, skuInfo.AlternateSkuInfos);
                    base.BindData2GridView(this.gvSubstituteSku, skuInfo.SubstituteSkuInfos);
                    base.BindData2GridView(this.gvPickLoc, skuInfo.InvSkuLocInfos);
                    this.txtSkuID.Enabled = false;
                    this.txtOwnerID.Enabled = false;
                    this.hdnSeqNo.Value = ((skuInfo.SubstituteSkuInfos.Count * 10) + 10).ToString();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetSkuInfo2Form(SkuInfo skuInfo)
        {
            this.txtSkuID.Text = skuInfo.SkuID;
            this.txtOwnerID.Text = skuInfo.OwnerID;
            this.chkIsActive.Checked = skuInfo.IsActive == "Y";
            this.chkShelfLifeIndicator.Checked = skuInfo.ShelfLifeIndicator == "Y";
            this.chkIsSalePromption.Checked = skuInfo.IsSalePromption == "Y";
            this.txtOwnerID.Text = skuInfo.OwnerID;
            this.txtSkuID.Text = skuInfo.SkuID;
            this.txtSkuClass.Text = skuInfo.SkuClassID;
            this.txtDescr.Text = skuInfo.Descr;
            this.txtHazmatCode.Text = skuInfo.HazmatCode;
            this.ddlShelfLifeCodeType.SelectedValue = skuInfo.ShelfLifeCodeType;
            this.txtDescr2.Text = skuInfo.Descr2;
            this.txtCustomSkuNo.Text = skuInfo.CustomSkuNo;
            this.txtShelfLifeOnReceiving.Text = skuInfo.ShelfLifeOnReceiving.ToString();
            this.txtPack.Text = skuInfo.PackID;
            this.txtCartonGroup.Text = skuInfo.CartonGroup;
            this.txtOriginCountry.Text = skuInfo.CountryOfOrigin;
            this.txtShelfLife.Text = skuInfo.ShelfLife.ToString();
            this.txtLotTemplate.Text = skuInfo.LotTemplateID;
            this.txtDaysToExpire.Text = skuInfo.DaysToExpire.ToString();
            this.txtLength.Text = base.GetPreferenceDigit(skuInfo.Length);
            this.txtWidth.Text = base.GetPreferenceDigit(skuInfo.Width);
            this.txtHeight.Text = base.GetPreferenceDigit(skuInfo.Height);
            this.txtCube.Text = base.GetPreferenceDigit(skuInfo.Cube);
            this.txtNetWeight.Text = base.GetPreferenceDigit(skuInfo.NetWgt);
            this.txtGrossWeight.Text = base.GetPreferenceDigit(skuInfo.GrossWgt);
            this.txtTareWeight.Text = base.GetPreferenceDigit(skuInfo.TareWgt);
            this.txtSize.Text = skuInfo.SkuSize;
            this.txtColor.Text = skuInfo.SkuColor;
            this.txtStyle.Text = skuInfo.SkuStyle;
            this.txtCostPrice.Text = base.GetPreferenceDigit(skuInfo.CostPrice);
            this.txtSalePrice.Text = base.GetPreferenceDigit(skuInfo.SalePrice);
            this.txtNetPrice.Text = base.GetPreferenceDigit(skuInfo.NetPrice);
            this.txtDefaultInUom.Text = skuInfo.DefaultInUom;
            this.txtDefaultOutUom.Text = skuInfo.DefaultOutUom;
            this.txtRFDefaultPack.Text = skuInfo.RfDefaultPack;
            this.ddlReciptValidationID.SelectedValue = skuInfo.ReceiptValidationID.ToString();
            this.ddlPutawayRuleID.SelectedValue = skuInfo.PutawayRuleID;
            this.ddlCalculatePutawayLocation.SelectedValue = skuInfo.CalculatePutawayLocation;
            this.ddlPreallocateRuleID.SelectedValue = skuInfo.PreallocateRuleID;
            this.ddlAllocateRuleID.SelectedValue = skuInfo.AllocateRuleID;
            this.ddlRotationRuleID.SelectedValue = skuInfo.RotationRuleID;
            this.txtABC.Text = skuInfo.Abc;
            this.ddlCCDiscrepanCyrule.SelectedValue = skuInfo.CcDiscrepanCyrule;
            this.txtLastCycleCount.Text = base.GetPreferenceShortDate(skuInfo.LastCycleCount);
            this.txtZoneID.Text = skuInfo.ZoneID;
            this.txtPutawayLoc.Text = skuInfo.PutawayLoc;
            this.txtReceiptInspectionLoc.Text = skuInfo.ReceiptInspectionLoc;
            this.txtQCLocOut.Text = skuInfo.QcLocOut;
            this.txtReturnsLoc.Text = skuInfo.ReturnsLoc;
            this.txtMinQty.Text = skuInfo.MinQty.ToString();
            this.txtMaxQty.Text = skuInfo.MaxQty.ToString();
            this.txtUserDefine1.Text = skuInfo.Udf1;
            this.txtUserDefine2.Text = skuInfo.Udf2;
            this.txtUserDefine3.Text = skuInfo.Udf3;
            this.txtUserDefine4.Text = skuInfo.Udf4;
            this.txtUserDefine5.Text = skuInfo.Udf5;
            this.txtUserDefine6.Text = skuInfo.Udf6;
            this.txtUserDefine7.Text = skuInfo.Udf7;
            this.txtUserDefine8.Text = skuInfo.Udf8;
            this.txtUserDefine9.Text = skuInfo.Udf9;
            this.txtUserDefine10.Text = skuInfo.Udf10;
            this.txtbudf1.Text = skuInfo.BUdf1;
            this.txtbudf2.Text = skuInfo.BUdf2;
            this.txtbudf3.Text = skuInfo.BUdf3;
            this.txtbudf4.Text = skuInfo.BUdf4;
            this.txtbudf5.Text = skuInfo.BUdf5;
            this.txtbudf6.Text = skuInfo.BUdf6;
            this.txtbudf7.Text = skuInfo.BUdf7;
            this.txtbudf8.Text = skuInfo.BUdf8;
            this.txtbudf9.Text = skuInfo.BUdf9;
            this.txtbudf10.Text = skuInfo.BUdf10;
            this.txtTariffID.Text = skuInfo.TariffID;
            this.txtRemark2.Text = skuInfo.Remark2;
            this.txtRemark1.Text = skuInfo.Remark1;
            this.txtSkuGroup.Text = skuInfo.SkuGroup;
            this.txtSkuGroup2.Text = skuInfo.SkuGroup2;
            this.ddlTransportMode.SelectedValue = skuInfo.TransportMode;
            this.txtReOrderQty.Text = base.GetPreferenceDigit(skuInfo.ReorderQty);
            this.txtReOrderPoint.Text = skuInfo.ReorderPoint.ToString();
            this.ddlFreightClass.SelectedValue = skuInfo.FreightClass;
            this.txtReceiptValidationTemplate.Text = skuInfo.ReceiptValidationTemplate;
        }

        protected void txtOwnerID_TextChanged(object sender, EventArgs e)
        {
            TradePartnerInfo owner = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.txtOwnerID.Text.Trim(), "TRADE_PARTNER") as TradePartnerInfo;
            if (owner != null)
            {
                this.ddlPutawayRuleID.SelectedValue = owner.DefaultPutawayRuleID;
                this.ddlCalculatePutawayLocation.SelectedValue = owner.CalculatePutawayLocation;
                this.ddlPreallocateRuleID.SelectedValue = owner.DefaultPreallocateRuleID;
                this.ddlAllocateRuleID.SelectedValue = owner.DefaultAllocateRuleID;
                this.ddlRotationRuleID.SelectedValue = owner.DefaultRotationRuleID;
                this.txtReceiptInspectionLoc.Text = owner.DefaultQcLoc;
                this.txtQCLocOut.Text = owner.DefaultQcLocOut;
                this.txtReturnsLoc.Text = owner.DefaultReturnsLoc;
            }
        }

        private void UpdateSkuInfo()
        {
            try
            {
                SkuInfo skuInfo = base.DataEntityViewState as SkuInfo;
                skuInfo = this.GetSkuInfoFromForm(skuInfo);
                skuInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(skuInfo, "SKU") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

